package com.jtjrenren.android.taxi.passenger.core.order.evalution;

import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;


import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiResultCommonStr;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.utils.ModuleAdapter;
import com.library.weidget.CustomeEvaluation;
import com.library.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/1/31.
 * 评价
 */
public class DriverEvaluationActivity extends BaseActivity {
    public static final String EXTRAS_DRIVERID = "driverid";
    public static final String EXTRAS_ORDERID = "orderid";

    public static final String RESULT_PJ = "pj_content";
    public static final String RESULT_PJ_LEVEL = "pj_level";


    @BindView(R.id.evaluation_level)
    CustomeEvaluation eva;
    @BindView(R.id.evaluation_level_desc)
    TextView tv_leveldesc;
    @BindView(R.id.evaluation_content)
    EditText et_content;
    @BindView(R.id.title)
    TextView tv_title;

    private String driver_id;
    private String order_id;

    @Override
    public int getLayoutId() {
        return R.layout.aty_driver_evaluation;
    }

    @Override
    public void init() {
        driver_id = getIntent().getStringExtra(EXTRAS_DRIVERID);
        order_id = getIntent().getStringExtra(EXTRAS_ORDERID);

        tv_title.setText(R.string.pingjia);

        setListeners();

        eva.setDefault();
    }

    private void setListeners() {
        eva.setChangeListener(new CustomeEvaluation.ChangeListener() {
            @Override
            public void changed(int level) {
                if (level==0)
                    return;
                String levelDesc = ModuleAdapter.getLevelDesc(level);
                tv_leveldesc.setText(levelDesc);
            }
        });
    }

    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }

    @OnClick(R.id.evaluation_commit)
    public void clickCommit() {
        commitEvaluation();
    }


    /**
     * 提交评价
     */
    private void commitEvaluation() {
        if (StringUtils.isEmpty(order_id) || StringUtils.isEmpty(driver_id)) {

            return;
        }
        int level = eva.level;
        if (level==0){
            displayShort("请选择星级");
            return;
        }

        level = ModuleAdapter.changeLocLevToServiceLevel(level);
        String contents = et_content.getText().toString().trim();

        if (contents != null && contents.length() > 100) {
            displayShort(getString(R.string.neirongbunengchaoguo100zifu));
            return;
        }

        showWaittingDialog(getString(R.string.shujutijiaozhongqingshaohou), true);


        ApiWorks.evaluationAdd(order_id, AppManger.getInstance().user.getWorkNo(), driver_id, level, contents,
                new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                    @Override
                    public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                        dismissWaittingDialog();
                        if (apiResultCommonStr != null) {
                            if (apiResultCommonStr.getCode() == 1) {
                                displayShort(getString(R.string.pingjiachenggong));
                                Intent data = new Intent();
                                data.putExtra(RESULT_PJ, et_content.getText().toString().trim());
                                data.putExtra(RESULT_PJ_LEVEL, ModuleAdapter.changeLocLevToServiceLevel(eva.level));
                                setResult(RESULT_OK, data);
                                finish();
                            } else if (apiResultCommonStr.getCode() == 0) {
                                displayShort(getString(R.string.pingjiashibai));
                            } else {
                                displayShort(getString(R.string.pingjiachenggong));
                                Intent data = new Intent();
                                data.putExtra(RESULT_PJ, et_content.getText().toString().trim());
                                data.putExtra(RESULT_PJ_LEVEL, ModuleAdapter.changeLocLevToServiceLevel(eva.level));
                                setResult(RESULT_OK, data);
                                finish();
                            }
                        } else {
                            displayShort(getString(R.string.pingjiashibai));
                        }
                    }
                });
    }
}
